<template>
  <el-row :gutter="20"
          id="designProductConsStyle">
    <el-form :model="requestParams"
             ref="form"
             :rules="rules"
             :disabled="todos==3?true:false"
             label-width="120px">
      <el-col :span="24">
        <el-form-item label="合同名称:"
                      size="medium"
                      :prop="todos==3?'':'contract_name'">
          <el-input v-model="requestParams.contract_name"
                    size="medium"
                    placeholder="请输入合同名称"></el-input>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="合同甲方:"
                      size="medium"
                      :prop="todos==3?'':'party_a'">
          <el-input v-model="requestParams.party_a"
                    size="medium"
                    placeholder="请输入合同甲方"></el-input>
          <!-- <el-select v-model="requestParams.party_a" filterable placeholder="请选择" size="medium" class="select-style">
    <el-option
      v-for="item in contrArr"
      :key="item.jt_org_id"
      :label="item.org_name"
      :value="item.jt_org_id">
    </el-option>
  </el-select> -->
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="生产基地名称:"
                      size="medium"
                      :prop="todos==3?'':'base_id'">
          <el-select v-model="requestParams.base_id"
                     filterable
                     placeholder="请选择"
                     size="medium"
                     class="select-style">
            <el-option v-for="item in baseArr"
                       :key="item.base_id"
                       :label="item.base_name"
                       :value="item.base_id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="施工项目名称:"
                      size="medium"
                      :prop="todos==3?'':'jt_project_id'">
          <!-- <el-input v-model="requestParams.jt_project_name" size="medium" placeholder="请输入施工项目名称" ></el-input> -->
          <el-select v-model="requestParams.jt_project_id"
                     filterable
                     placeholder="请选择"
                     size="medium"
                     class="select-style">
            <el-option v-for="item in conProArr"
                       :key="item.jt_project_id"
                       :label="item.project_name"
                       :value="item.jt_project_id">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="方量(m³):"
                      size="medium"
                      :prop="todos==3?'':'amount'">
          <el-input-number v-model="requestParams.volume"
                           controls-position="right"
                           placeholder="请输入方量(m³)"
                           style="width:220px;"
                           @change="handleChange"
                           :precision="2"
                           :step="0.1"
                           :min="0"
                           :max="1000000"
                           size="medium"></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="合同金额(万元):"
                      size="medium"
                      :prop="todos==3?'':'amount'">
          <el-input-number v-model="requestParams.amount"
                           controls-position="right"
                           placeholder="请输入合同金额(万元)"
                           style="width:220px;"
                           :precision="2"
                           :step="0.1"
                           :min="0"
                           :max="1000000"
                           size="medium"></el-input-number>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="订单类型:"
                      size="medium"
                      :prop="todos==3?'':'order_type'">
          <el-select v-model="requestParams.order_type"
                     filterable
                     placeholder="请选择"
                     size="medium"
                     class="select-style">
            <el-option v-for="item in ordetTypeArr"
                       :key="item.value"
                       :label="item.name"
                       :value="item.value">
            </el-option>
          </el-select>
        </el-form-item>
      </el-col>
      <el-col :span="24">
        <el-form-item label="时间:"
                      size="medium"
                      :prop="todos==3?'':'add_time'"
                      label-width="120px">
          <el-date-picker v-model="requestParams.add_time"
                          type="datetime"
                          value-format="yyyy-MM-dd HH:mm:ss"
                          placeholder="选择日期">
          </el-date-picker>
        </el-form-item>
      </el-col>
      <el-col :span="24"
              class="bottom-box"
              v-show="todos==3?false:true">
        <el-button size="medium"
                   @click="onCancel">取消</el-button>
        <el-button type="primary"
                   size="medium"
                   @click="onSure('form')">确定</el-button>
      </el-col>
    </el-form>
  </el-row>
</template>

<script>
import moment from 'moment'
import { ProductManageApi, DesignManageApi, ConstrucManagementApi } from '@/services/WorkCenter'
import { standardCodeApi } from '@/services/StandardCode'
export default {
  props: {
    todos: Number,
    catchBack: Function,
    catchSave: Function,
    detailObj: Object
  },
  data () {
    return {
      contrArr: [],
      baseArr: [],
      conProArr: [],
      ordetTypeArr: [],
      requestParams: {
        volume: undefined,
        contract_name: '',
        party_a: '',
        base_id: '',
        jt_project_id: '',
        amount: undefined,
        order_type: '',
        add_time: ''
      },
      rules: {
        contract_name: [
          { required: true, message: '合同名称不能为空', trigger: 'change' }
        ],
        party_a: [
          { required: true, message: '合同甲方不能为空', trigger: 'change' }
        ],
        base_id: [
          { required: true, message: '生产基地名称不能为空', trigger: 'change' }
        ],
        jt_project_id: [
          { required: true, message: '施工项目名称不能为空', trigger: 'change' }
        ],
        // jt_project_name:[
        // {required:true,message:'施工项目名称不能为空',trigger:'change'}
        // ],
        volume: [
          { required: true, message: '方量(m³)不能为空', trigger: 'change' }
        ],
        amount: [
          { required: true, message: '合同金额（万元）不能为空', trigger: 'change' }
        ],
        order_type: [
          { required: true, message: '订单类型不能为空', trigger: 'change' }
        ],
        add_time: [
          { required: true, message: '创建日期不能为空', trigger: 'blur' }
        ]
      }
    }
  },
  methods: {
    //获取施工项目下拉
    getConsProjectList () {
      return ConstrucManagementApi.getConsProjectList({
        params: {
          type: 3,//1设计项目，2生产项目，3施工项目，（默认3）
        }
      }).then((res) => {
        this.conProArr = res.data.list;
      }).catch((error) => {
        console.log(error)
      })
    },
    //   getOrgList(){//获取甲方下拉列表
    //  return DesignManageApi.getOrgList({
    //    params:{
    //     // type:1,//1设计，2生产，3施工
    //     list_or_tree:'list'//获取list或者tree
    //    }
    //  }).then((res)=>{
    //    this.contrArr=[];
    //    this.contrArr=res.data.list;
    //  }).catch((error)=>{
    //    console.log(error)
    //  })
    // },
    //获取生产基地列表数据
    getBaseData () {
      return ProductManageApi.getBaseList({
        params: {
          is_page: 0
        }
      }).then((res) => {
        this.baseArr = res.data.list;
      }).catch((error) => {
        console.log(error)
      })
    },
    // 获取标准码
    getStardandCode () {
      return standardCodeApi.getStandardCodeData({
        data: {
          code_type: 'proOrderType'
        }
      }).then((res) => {
        this.ordetTypeArr = res.data.list
      }).catch((error) => {
        console.log(error)
      })
    },
    handleChange (value) {
      console.log(value);
    },
    //取消
    onCancel () {
      this.$refs['form'].resetFields();
      this.catchBack();
    },
    //确定
    onSure (data) {
      this.$refs[data].validate((valid) => {
        if (valid) {
          if (this.todos == 1) {
            ProductManageApi.addProductOrder({
              data: this.requestParams
            }).then((res) => {
              if (res.code == 0) {
                this.$message.success('操作成功')
                this.catchSave()
              }
            }).catch((error) => {
              console.log(error)
            });
          } else if (this.todos == 2) {
            this.requestParams.id = this.detailObj.order_id;//主键值
            ProductManageApi.editProductOrder({
              data: this.requestParams
            }).then((res) => {
              if (res.code == 0) {
                this.$message.success('操作成功')
                this.catchSave()
              }
            }).catch((error) => {
              console.log(error)
            });
          }
        } else {
          console.log('error')
        }
      })
    },
    //查看详情
    getDetailOrder () {
      return ProductManageApi.getDetailOrder({
        params: {
          id: this.detailObj.order_id
        }
      }).then((res) => {
        this.requestParams = res.data;//获取详情
        this.requestParams.order_type = String(this.requestParams.order_type)
      }).catch((error) => {
        console.log(error)
      })
    }
  },
  watch: {//监听
    todos (n, o) {
      if (n == 0) {
        this.requestParams.add_time = moment().format('YYYY-MM-DD:HH:MM:SS');//获取当前系统时间
        this.requestParams.contract_name = '';
        this.requestParams.volume = undefined
        this.$refs['form'].resetFields();
      } else {
        this.getConsProjectList();//施工项目下拉
        // this.getOrgList();//甲方下拉
        this.getBaseData();//生产基地名称
        this.getStardandCode();//获取订单类型
      }
      if (n == 2 | n == 3) {
        this.getDetailOrder();//查看详情
      }
      console.log('watch-todos=', n, 'this.requestParams=', this.requestParams)
    }
  },
  async created () {
    this.requestParams.add_time = moment().format('YYYY-MM-DD:HH:MM:SS');//获取当前系统时间
    await this.getConsProjectList();//施工项目下拉
    // await this.getOrgList();//甲方下拉
    await this.getBaseData();//生产基地名称
    await this.getStardandCode();//订单类型
    if (this.todos == 2 | this.todos == 3) {
      await this.getDetailOrder();//查看详情
    }
    console.log('created-todos=', this.todos, 'this.requestParams=', this.requestParams)
  }
}
</script>

<style lang="scss" scoped>
.select-style {
  width: 100%;
}
.bottom-box {
  text-align: right;
  margin-top: 4px;
}
.select-style {
  width: 100% !important;
}
.el-col-12 {
  padding-left: 0px !important;
}
p {
  margin-top: 18px;
  font-size: 14px;
  font-family: Microsoft YaHei;
  font-weight: 600;
  color: rgba(0, 0, 0, 0.85);
  opacity: 1;
}
</style>